	import java.awt.Color;
	import java.awt.Dimension;
	import java.util.List;
	import javax.swing.JPanel;
	import javax.swing.JTable;
	import javax.swing.JScrollPane;
	import javax.swing.table.TableColumn;
	import java.util.Collections;
	import javax.swing.table.DefaultTableCellRenderer;
	import java.awt.Component;
	
	public class PhoneDayLayout {
	
	  private PhoneLayout p;
	  private JPanel cards;
	  private CalendarDate currentDay;
	  private List<Appointment> appointments;
	
	  public PhoneDayLayout(PhoneLayout phone) {
	    p = phone;
	    cards = phone.middleCards;
	
	    JPanel dayView = new JPanel();
	    dayView.setBackground(Color.white);
	    dayView.setPreferredSize(new Dimension(480,640));
	
	    currentDay = phone.date;

	    p.dateLabel.setText( "<html>    <font size=\"5\">" + CalendarDate.getDay( currentDay.day, currentDay.month, currentDay.year ) + "</font>" + "<p>   " + p.getDateString() + "</html>");
	
	    appointments = phone.cal.getAppointmentsBetweenDates( currentDay, currentDay );
	    Collections.sort( appointments );
	
	    String[] columnNames = new String[]{ "Time", "Appointment" };
	    Object[][] data = new Object[24][2];
	
	    for ( int hour = 0; hour < 24; hour++ ) {
	      data[ hour ][ 0 ] = hourString( hour );
	
	      for ( Appointment ap : appointments ) {
	        if ( appointmentHour( hour, ap.start_time, ap.end_time ) ) {
	          data[ hour ][ 1 ] = "<html><b>" + ap.description + "</b><p>" + ap.location + "</html>";
	        }
	      }
	    }
	
	    JTable table = new JTable( data, columnNames );
	    table.setDefaultRenderer(Object.class, new TableRenderer( data ));
	    table.setFillsViewportHeight(true);
	    table.setRowHeight( 40 );
	    table.setEnabled( false );
	
	    TableColumn column = null;
	    for ( int i = 0; i < 2; i++ ) {
	      column = table.getColumnModel().getColumn( i );
	      if ( i == 0 ) {
	        column.setPreferredWidth( 100 );
	      }
	      else if ( i == 1 ) {
	        column.setPreferredWidth( 380 );
	      }
	    }
	
	    JScrollPane scrollPane = new JScrollPane( table );
	    scrollPane.setPreferredSize( new Dimension(480,635));
	
	    dayView.add(scrollPane);
	
	    cards.add(dayView, "Day");
	  }
	
	  public static boolean appointmentHour( int hour, CalendarTime start, CalendarTime end ) {
	    int startHour = Integer.parseInt( timeStr( start ).substring( 0, 2 ) );
	    int endHour =  Integer.parseInt( timeStr( end ).substring( 0, 2 ) );
	
	    return ( hour >= startHour && hour < endHour );
	  }
	
	  public static String timeStr( CalendarTime time ) {
	    String timeStr[] = time.toString().split(":");
	
	    if ( timeStr[ 0 ].length() < 2 ) {
	      timeStr[ 0 ] = "0" + timeStr[ 0 ];
	    }
	
	    if ( timeStr[ 1 ].length() < 2 ) {
	      timeStr[ 1 ] = "0" + timeStr[ 1 ];
	    }
	
	    return ( timeStr[ 0 ] + ":" + timeStr[ 1 ] );
	  }
	
	  public static String hourString( int hour ) {
	    if ( hour < 10 ) {
	      return ( "0" + hour + ":00" );
	    }
	
	    return ( hour + ":00" );
	  }
	
	  public class TableRenderer extends DefaultTableCellRenderer {
	
	    /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		private Object[][] data;
	
	    public TableRenderer( Object[][] data ) {
	      this.data = data;
	    }
	
	    public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean f, int row, int column ) {
	      Component c = super.getTableCellRendererComponent( table, value, isSelected, f, row, column );
	
	      if ( this.data[ row ][ 1 ] != null ) {
	        c.setBackground( Color.pink );
	      }
	      else {
	        c.setBackground( Color.white );
	      }
	      return ( c );
	    }
	  }
	}
